package com.mobile.liuyi_s.orderlunch.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.Utils.UserUtils;
import com.mobile.liuyi_s.orderlunch.Utils.Utils;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class SignUpActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "SignUpActivity";
    //set hide softKeyBoard
    private InputMethodManager mInputMethodManager;

    private EditText mEditTextUserName;
    private EditText mEditTextPwd;
    private EditText mEditTextPwdConfirm;
    private Button mButton;
    private Toolbar mToolbar;

    private String userPwd;
    private String userPwdConfirm;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        initView();
        mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
    }

    private void initView() {
        initToolbar();
        mEditTextUserName = ((EditText) findViewById(R.id.sign_up_user_name));
        mEditTextPwd = ((EditText) findViewById(R.id.sign_up_user_pwd));
        mEditTextPwdConfirm = ((EditText) findViewById(R.id.sign_up_confirm_user_pwd));
        mButton = ((Button) findViewById(R.id.btn_sign_up));
        mButton.setOnClickListener(this);
    }
    private void initToolbar() {
        mToolbar = ((Toolbar) findViewById(R.id.toolbar_sign_up));
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.btn_sign_up:
                userName = mEditTextUserName.getText().toString().trim();
                userPwd = mEditTextPwd.getText().toString().trim();
                userPwdConfirm = mEditTextPwdConfirm.getText().toString().trim();

                Utils.hideKeyBoard(this,mInputMethodManager);

                if (userName.length() >= 2) {
                    if (userPwd.length() >=3 && userPwdConfirm.length() >= 3) {
                        if (userPwd.equals(userPwdConfirm)) {
//                    queryUserIsExist();
                            signUp(userName,userPwd);
                        } else {
                            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "密码最少三位", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "用户名最少两位", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    private void signUp(String name, String password) {
        MyUser myUser = new MyUser();
        myUser.setUsername(name);
        myUser.setPassword(password);
        myUser.signUp(new SaveListener<MyUser>() {
            @Override
            public void done(MyUser myUser, BmobException e) {
                if (e == null) {
                    Toast.makeText(SignUpActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    SignUpActivity.this.finish();
                } else {
                    Toast.makeText(SignUpActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
//    //query username form DB, if not exist, sign up
//    private void queryUserIsExist() {
//        BmobQuery<MyUser> query = new BmobQuery<>();
//                    query.addWhereEqualTo("userName",userName);
//                    query.findObjects(new FindListener<MyUser>() {
//                        @Override
//                        public void done(List<MyUser> list, BmobException e) {
//                            if (e == null) {
//                                if (list.size() == 0) {
//                                    UserUtils userUtils = new UserUtils();
//                                    userUtils.signUp(SignUpActivity.this, userName, userPwdConfirm);
//                                } else {
//                                    Toast.makeText(SignUpActivity.this, "用户已注册", Toast.LENGTH_SHORT).show();
//                                }
//                            } else {
//                                Log.d(TAG, "查询用户信息失败");
//                                Toast.makeText(SignUpActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
//                            }
//                        }
//                    });
//    }
}
